package com.leadingsoft.pwxk.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * 四川环保项目加解密类 用于四川环保项目数据交互时加解密使用
 *
 * @author sunzhe
 * @Create 2018-06-21 该类用于加解密时使用
 *
 */
public final class UnifyEncriptSCHB extends Base64 {

	public static final String KEY_ALGORITHM_AES = "AES";

	public static final String KEY_STR = "T9kCFFCBcNb6qhgHlQ1qwQ==";

	public static String encrypt(String dataStr) {
		SecretKey secretKey = restoreAESKey(KEY_STR);
		// 初始化加密组件
		Cipher cipher;
		try {
			cipher = Cipher.getInstance(KEY_ALGORITHM_AES);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey);
			// 加密后的数据，首先将字符串转为byte数组，然后加密，为便于保存先转为base64
			String encryptedDataStr = encodeBase64String(cipher.doFinal(dataStr.getBytes()));
			return encryptedDataStr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String decrypt(String encryptedDataStr) {
		SecretKey secretKey = restoreAESKey(KEY_STR);
		try {
			Cipher cipher = Cipher.getInstance(KEY_ALGORITHM_AES);
			// 将加密组件的模式改为解密
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			// 和上面的加密相反，先解base64，再解密，最后将byte数组转为字符串
			String decryptedDataStr = new String(cipher.doFinal(Base64.decodeBase64(encryptedDataStr)), "UTF-8");
			return decryptedDataStr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static SecretKey restoreAESKey(String base64EncodedAESKey) {
		// 还原秘钥字符串到秘钥byte数组
		byte[] keyByteArray = decodeBase64(base64EncodedAESKey);
		// 重新形成秘钥，SecretKey是Key的子类
		SecretKey secretKey = new SecretKeySpec(keyByteArray, KEY_ALGORITHM_AES);
		return secretKey;
	}

	public static void main(String[] args) {
		String str = "我是中国文人";
		System.out.println("原字符串：" + str);
		String aesEncryptedStr = UnifyEncriptSCHB.encrypt(str);
		System.out.println("加密后：" + aesEncryptedStr);
		String aesDecryptedStr = UnifyEncriptSCHB.decrypt("E7C906A12EC21BE85820DD249F4652FA89FAF7A9AEB0CD2007FBE4D7519253634B94968CA77E53C4DB0ECFFD3EC2603E3F3FFED4B73D8F3636B681191FF913303EF14971C92E0A80BD309183E18196AF83AABCD5AFF6A73568E7893A3BA92A65FB231D59A6A02B9C42A2C482BDC7950AC390CB5964B40ACBB4BD2525C52F101CCC8E52507A82CE653098F0C779C20F41C23346E12A5EAE99AF69466565F4A62C65EAAB07A72B5AB60D0A695701F6617123605B1A3DFBA85060EAD5F9EA25821DA04FD3BE5F22BC24F0769766A8E4D704418030708354E3D0C2354E70BC48C8601242EFBA2CBBB47A7655F76EE158B6B940A7826828214F08A13B666839BA360C2452490881071C9F7FD26DFC64F3E1153CB0AF383911F281B7161EAF64EE219F70F332032F3637EE4F8269B1CAE62C9344CD9FDA75880C4BD5E0051BE1585C0C06ABAE1B4ED858C7695E2600C4C33B64369ABF72580DCD603633D335CF5AEA694CCFEA209578D0449CAF4B73B515060BA7D355A9429D01B7290A289ABA7B2DCD1D961AB89D1843BFA4420DC34C700F789290C2B7A1ED0E8C7C9B16EEB3E0799B495B5EAF115F865BC9621F8C09EAB6E2C53A6F04827E75A557FEB6179A510EB34B03788BB8568F416CB7E395F98609442DA4FFD3C0471639092BCFAD4610AC3CFF1ECFD9DF3B8BDE13121A57F01A06A46FDD73262E15FF257A5CD5048123CB350847A57FFADAD14CDAFF05E04081A858BFDD2FFE9A0C240751A0B8262C1901949FBCEC2FD32EA589F5FD45E478F11DEC071D0B53FA8FFCB90FCD294F9C5B81E6B4648EB7F0C9119A5A27429DD2F57AF346CE25E0B2514416B29132BF8A481B983257DDD287608749409323C491F53CA198423EEA74AB9F18307A0DB22562AA5444450098B812F8261428F3CF5B5DE3D98578A91594705D3B9F0E0F656DCCE92077F94E255167E15A3B5E2BC45D485653422D7E7A0E87B34A61CE16A6B6C2F151465206C683644BF5F836C28796287013232AC0156BE0B718133F01BC2BE8AAECCDB064B3F4ED57FDD29F9ECDFCB9A83FAF424FE053DE535416123F8A304FBE5DCBF395E33FB7F95E72DEABF4D5BECA4D7517B6BCF34DF551D7502971E20EA8851A6CE974555A901C99925E1F3D89AF445930D7964D5F12928A289DA404F6D58572618B2AB1A5F650416FDD7F357DF230EFDF2B10EC98C5591EF39ED4336F7A3E541D47B60D6396A8D703E2792031F7CF0A8E4AB2F0001BB06D800441735BC83C11BE404261003769401B9AB3E40A7B2969D5E2B9CFB3992A15888059547947D7B4F57F772468F3AD15C1D032E817F0EFCD11FA43A052E8CED9408990D81C9B7362BE3BEA94943E3F731303BF39661B071702F7D5459A45B996AF9BC3E31E32CFA2D0AC7F362EEEA76D7F908881709CBA0147DE3BC666B0E6A662C9846F17B9B2156B00F22B9E360B1E60599262880A6A5D1B924EAC929EA5A4B65A42C9CAEFF7C3EAED270342F746D54CE7732A45205BB4A0307AB123747F49CA27DBECD74911FEC29C8B80BFF8B9A48A4267301BD4C12BFDAA4F4AA191276CA934D598C9F808CB2B9F7E61D190A3866135ECB58F3BFE");
		System.out.println("解密后：" + aesDecryptedStr);

	}

}
